/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.run;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.bluestone.BaseAction;
import com.bluestone.action.ActionFactory;
import com.bluestone.assertor.AssertorFilter;
import com.bluestone.assertor.IAssertor;
import com.bluestone.config.AssertorConfig;
import com.bluestone.config.ConfigUtil;
import com.bluestone.config.FilterConfig;
import com.bluestone.context.ActionContext;
import com.bluestone.context.CommandContext;
import com.bluestone.context.ConfigContext;
import com.bluestone.context.ExecuteContext;
import com.bluestone.context.IContext;
import com.bluestone.context.ProjectContext;
import com.bluestone.context.TestCaseContext;
import com.bluestone.context.TestSuiteContext;
import com.bluestone.report.ActionProxy;
import com.bluestone.report.ExportTool;
import com.bluestone.report.ProjectReportBean;
import com.bluestone.report.ProjectReportDataSource;
import com.bluestone.scripts.ActionScript;
import com.bluestone.scripts.Attributes;
import com.bluestone.scripts.Command;
import com.bluestone.scripts.Execute;
import com.bluestone.scripts.Project;
import com.bluestone.scripts.Property;
import com.bluestone.scripts.TestCase;
import com.bluestone.scripts.TestSuite;
import com.bluestone.util.Util;

/**
 * 
 * @author <a href="mailto:bluestone.daniel.q@gmail.com">daniel.q</a> 
 */
public class RunManager extends Thread{

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String file = "./scripts/section508.xml";
		if (args.length > 0) {
			file = args[0];
		}
		RunManager manager = new RunManager();
		try {
			manager.startup(file);
		} catch (Exception me) {
			me.printStackTrace();
		}
	}

	ProjectContext projectContext;
    private static boolean shutdown = false;
    
	public void shutdown() {
		shutdown = true;
		Iterator iterator = ConfigContext.getInstance().getAssertors().values()
				.iterator();
		while (iterator.hasNext()) {
			IAssertor assertor = (IAssertor) iterator.next();
			assertor.stop();
		}
	}

	public void startup(String entryFile) {
	    shutdown = false;
	    
		projectContext = ProjectContext.getInstance(entryFile);

		new ProjectController().execute(projectContext);
		while(true){
			if(shutdown){
				break;
			}
			try {
				this.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
